define(['app','js/directive/listTree','js/filter/tree2Arr','datetime','autocomplete','pagination'], function( appCRM ) {
	'use strict';
	/*服务人员新单、续费查账*/
	appCRM.controller('Pays', function($scope,$filter ,dataFactoryCRM, $q, crmConst ) {
		$scope.payslist;
		$scope.payspage = {pageAll: 1, pageNow: 1,pageSize:9,k: 333};
		$scope.educationTypes = crmConst.EDUCATION;
		$scope.careerTypes = crmConst.CAREER;
		$scope.tab = 'pays';
		$scope.prev_search;
		$scope.baseUrl = crmConst.BASE + crmConst.API;/*semantic form validate unique user data*/
		$scope.pageParam = {prices:[],products:[],xufei:[],customer:[]};


		$scope.search_param = {name:'',phone:'',pay_name:'',audit:-1,stime:'',etime:''};
		$scope.data_new = {birthday:'',contract_time:'',pay_time:''};
		$scope.data_xufei = {customer_id:-1,pay_name:'',pay_mount:'',pay_way:-1,pay_time:'',note:'',
							 product:0,price:'',contract_start_time:'',customer_buycount:1,xufei_type:0};
		$scope.data_xindan = {};
		$scope.data_xufei1 = {};

		$scope.customer_name_cat; 
		$scope.customerInfo = false;

        /*系统错误调用*/
        $scope.msgAttention = function() {
            $('.ui.systemerror').modal({
                closable: true,
                onDeny: function() {
                    return true;
                },
                onApprove: function() {
                    $('.ui.dimmer.modals.page.transition.visible.active.dimmers').removeClass('dimmers');
                }
            }).modal('show');

            setTimeout(function() {
                var system = $('.ui.dimmer.modals.page.transition.visible.active');
                if (system) {
                    $('.ui.dimmer.modals.page.transition.visible.active').addClass('dimmers');
                }
            }, 1000);
        };

		$scope.selectXufei = function( selected ){
			if( selected ){
				$('#customer_sales').val( selected.originalObject.user_id );
			}
		}

		// 续费编辑查账
		$scope.selectXufei1 = function( selected ){
			if( selected ){
				$('#customer_sales_edit').val( selected.originalObject.user_id );
			}
		}
		$scope.enterSearch = function( evt ){
			if( evt && evt.keyCode == 13){
				$scope.getPaysList();
				console.log("enter search");
			}
		}

		$scope.search_audit = function( v ){
			$scope.search_param.audit = v;
			$scope.search_param.name = '';
			$scope.search_param.pay_name = '';
			$scope.search_param.phone = '';
			$scope.search_param.stime = '';
			$scope.search_param.etime = '';
			$scope.getPaysList();
		}

		// 添加退款获取合同点击事件
		$scope.selectCustomer_fedbak = function( selected ){
			if (selected) {
				var cid_fed = selected.originalObject.customer_id;
				$('#customer_fedbak').val( cid_fed );
				dataFactoryCRM.postDataByAPI('pays/products','customer=' + cid_fed)
	            .then(function(d) {
	              if (d.rcode) {
		              	$scope.customerInfo_fedbak = d.data;
		              	$scope.customerVerify_fedbak = 1;
	              } else {
	                $scope.customerInfo_fedbak = {};
	                $scope.data_xufei.customer_id = -1;
	                $scope.customerVerify_fedbak = 0;
	              }
	            });
			}
		}

		$scope.input_default = function(){
			for(var i in $scope.data_xufei){
				$scope.data_xufei[i] = '';
 			}
			$scope.data_xufei.customer_buycount = 1;
			$scope.data_xufei.pay_time = $filter('date')(new Date(), 'yyyy-MM-dd HH:mm');
			$scope.data_xufei.contract_start_time = $filter('date')(new Date(), 'yyyy-MM-dd');
			$scope.data_new.pay_time = $filter('date')(new Date(), 'yyyy-MM-dd HH:mm');
			$scope.data_new.contract_time = $scope.data_xufei.contract_start_time;
			$scope.data_new.birthday = '1987-03-03';
			$scope.customerInfo = false;

		}

		$scope.selectCustomer = function( selected ){
			if( selected ){
				var cid = selected.originalObject.customer_id;
				$('#xufei_customer').val( cid );//添加续费顾客id
				$('#customer_fedbak_edit').val(cid);
				$scope.data_xufei.customer_id = cid;
				dataFactoryCRM.postDataByAPI('pays/products','customer='+cid)
	            .then(function(d) {
	            	// console.log(d);
	              if (d.rcode) {
		              	$scope.customerInfo = d.data;
		              	$scope.customerVerify = 1;
		              	$scope.ConState = d.data.state;
		                $scope.d_times = d.data.d_time;
		                $scope.Pro_id = d.data.product_id;
	              } else {
	                $scope.customerInfo = {};
		            $scope.customerVerify = 0;
	                $scope.data_xufei.customer_id = cid;
	                 $scope.d_times = 4;
	              }
	            });
			}
		}

		//编辑新单查账
		$scope.selectCustomer1 = function( selected ){
			if( selected ){
				var cid = selected.originalObject.customer_id;
				$('#xufei_customer_edit').val( cid );
				dataFactoryCRM.postDataByAPI('pays/getname','customer='+cid)
	            .then(function(d) {
	            	$scope.data_xufei1.customer_name = d.nickname;
	            });
				$scope.data_xufei.customer_id = cid;
				dataFactoryCRM.postDataByAPI('pays/products','customer='+cid)
	            .then(function(d) {
	              if (d.rcode) {
		              	$scope.customerInfo1 = d.data;
		              	$scope.customerVerify1 = 1;
		              	$scope.ConState_xufei = d.data.state;
	              } else {
	                $scope.customerInfo1 = {};
		            $scope.customerVerify1 = 0;	                
	                // $scope.data_xufei.customer_id = -1;
	              }
	            });
			}
		}	 


		$scope.getPaysList = function(){
			$scope.$emit("BUSY");
			$scope.payspage.pageSize = 11;
			var _url = 'pays/index?psize='+$scope.payspage.pageSize+'&p='+$scope.payspage.pageNow +'&'+$.param( $scope.search_param ) +'&Stime='+$scope.search_param.stime;
			// if( $scope.prev_search == _url )
			// 	return false;
			dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
		      if (_d.rcode) {
		      		$scope.prev_search = _url;
		        	$scope.payspage.pageSize = _d.psize;
		        	$scope.payspage.pageAll = _d.pall;
		        	$scope.payspage.pageNow = _d.p;
		        	$scope.payslist = _d.data;
		      } else {
		        // alert(_d.msg);
                $scope.payspage.pageAll = 0;
		      }
		      $scope.$emit("NOTBUSY");
		    });
		}


		$scope.getOptions = function(){
			var _url = 'pays/paytypes';
			dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
		      if (_d.rcode)
		    	$scope.pageParam.payways = _d.data;
		    });

		    /*get user sectors*/
		    _url = 'sector/index';
		    dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
		      if (_d.rcode)
		    	$scope.pageParam.sectors = _d.data;
		    });
		    /*get user source*/
		    _url = 'source/mysource';
		    dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
		      if (_d.rcode){
		      	$scope.pageParam.sources = _d.data;
		      }
		    });

		    /*get user source jzd*/
		    _url = 'pays/Gtype';
		    dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
		    	console.log(_d);
		      if (_d.rcode){
		      	$scope.pageParam.customers_rt = _d.data;
		      }
		    });

		    // _url = 'pays/twiceCon';
		    // dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
		    //   if (_d.rcode){
		    //   	$scope.pageParam.products_twice = _d.list.product_list;
		    //   	$scope.pageParam.products_twicenew = _d.list.product_list;
		    //   }
		    // });		    

		    /*get product & price & xufei & customer */
		     _url = 'pays/products';
		    dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
		      if (_d.rcode){
		      	$scope.pageParam.products = _d.data.product;
		      	$scope.pageParam.prices = _d.data.price;
		      	$scope.pageParam.xufei = _d.data.xufei;
		      	$scope.pageParam.customers = _d.data.customer;
		      }
		      else
		        alert(_d.msg);
		    });
		}

		//二级联动
		$scope.Upgrade_Proudct = function(selected){
			$("#xufei_price").text("选定价格规则");
			$('#xufei_neworder').text("选定价格规则");
			$scope.selected = selected;
			for (var i = 0; i < $scope.pageParam.products_twice.length; i++) {
				if(i == $scope.selected ){
					$scope.connection = $scope.pageParam.products_twice[i].price_list;
					$scope.connectionNew = $scope.pageParam.products_twice[i].price_list;
				}
			};	
		}	

		// 添加新单查账
		$scope.newOrder = function(){
			// $('.ui.add').modal("refresh");
			$('#frm-addnew input').val('');	
			$('.ui.selection.dropdown').dropdown('restore defaults');
			function add( data ){
				$scope.busy = dataFactoryCRM.postDataByAPI('pays/addnew', data )
				      .then(function(d) {
				        if ( d && d.rcode) {			        	
				          $('#frm-addnew input').val('');
				          $scope.getPaysList();
				          return true;
				        }
				        else{
				          	return false;
				        }
				      });
				      $scope.getPaysList();
			}
			$('.ui.add').modal({
		      onApprove: function() {
		        if( !$('#frm-addnew').form('is valid') ){
		            return false;
		        }
		        else{
		        	add( $('#frm-addnew').serialize() );
		        }
		        return true;
		      }
		    }).modal('show');
		}

		// 添加续费查账
		$scope.xufeiOrder = function(){
			function xufei( data ){
				$scope.busy = dataFactoryCRM.postDataByAPI('pays/addxufei', data )
				      .then(function(d) {
				      	// console.log(d);
				        if ( d && d.rcode) {
				          	$('#frm-addxufei input').val('');
				          	 $scope.input_default();
				          	 $scope.getPaysList();
				          	return true;
				        }
				        else
				          	return false;
				      });
				      $scope.getPaysList();
			}
			$('#frm-addxufei input').val('');
			$scope.input_default();
			$('.ui.selection.dropdown').dropdown('restore defaults');
			// $('.ui.xufei').modal("refresh");
			$('.ui.xufei').modal({
		      onApprove: function() {
		      	// console.log($scope.d_times);
		        if( $('#frm-addxufei').form('is valid')){
		        	if ($scope.d_times == 1) {
		        		if ($scope.ConState == -1 || $scope.ConState == 2 || $scope.ConState == 3) {//合同不在运行状态
		        			if ($('input[name="xufei[xufei_type]"]').val() == 1) {
					        	$scope.data_xufei.product = $('input[name="xufei[product]"]').val();
					        	$scope.data_xufei.price = $('input[name="xufei[price]"]').val();
					        	$scope.data_xufei.xufei_type = $('input[name="xufei[xufei_type]"]').val();
					        	$scope.data_xufei.pay_way= $('input[name="xufei[pay_way]"]').val();
					        	if( $scope.customerInfo )
					        		$scope.data_xufei.contract_id = $scope.customerInfo.contract_id;
					        	xufei( $scope.data_xufei );
					        	return true;		        				
		        			}else{
				        		$('#xufei_type_add').addClass('error');
					  			var div=document.createElement("div");
			   					var diva = document.getElementById('xufei_add_sty');
			   					diva.parentNode.insertBefore(div, diva);			    
				        		$('#xufei_add_sty').text('续费类型只能为前续');
				        		$('#xufei_add_sty').addClass('ui basic red pointing prompt label transition visible');
					        	return false;			        				
		        			}

		        		}else{
			        		if ($('input[name="xufei[product]"]').val() == $scope.Pro_id && $('input[name="xufei[xufei_type]"]').val() == 0) {
					        	$scope.data_xufei.product = $('input[name="xufei[product]"]').val();
					        	$scope.data_xufei.price = $('input[name="xufei[price]"]').val();
					        	$scope.data_xufei.xufei_type = $('input[name="xufei[xufei_type]"]').val();
					        	$scope.data_xufei.pay_way= $('input[name="xufei[pay_way]"]').val();
					        	if( $scope.customerInfo )
					        		$scope.data_xufei.contract_id = $scope.customerInfo.contract_id;
					        	xufei( $scope.data_xufei );
					        	return true;	
			        		}else if($('input[name="xufei[product]"]').val() != $scope.Pro_id){
					        	$scope.data_xufei.product = $('input[name="xufei[product]"]').val();
					        	$scope.data_xufei.price = $('input[name="xufei[price]"]').val();
					        	$scope.data_xufei.xufei_type = $('input[name="xufei[xufei_type]"]').val();
					        	$scope.data_xufei.pay_way= $('input[name="xufei[pay_way]"]').val();
					        	if( $scope.customerInfo )
					        		$scope.data_xufei.contract_id = $scope.customerInfo.contract_id;
					        	xufei( $scope.data_xufei );
					        	return true;
			        		}else{
				        		$('#xufei_type_add').addClass('error');
					  			var div=document.createElement("div");
			   					var diva = document.getElementById('xufei_add_sty');
			   					diva.parentNode.insertBefore(div, diva);			    
				        		$('#xufei_add_sty').text('续费类型只能为补款');
				        		$('#xufei_add_sty').addClass('ui basic red pointing prompt label transition visible');
					        	return false;		        			
			        		}		        			
		        		}
		        	}else if($scope.d_times == 2){
		        		if ($scope.ConState == -1 || $scope.ConState == 2 || $scope.ConState == 3) {
		        			if ($('input[name="xufei[xufei_type]"]').val() == 1) {
					        	$scope.data_xufei.product = $('input[name="xufei[product]"]').val();
					        	$scope.data_xufei.price = $('input[name="xufei[price]"]').val();
					        	$scope.data_xufei.xufei_type = $('input[name="xufei[xufei_type]"]').val();
					        	$scope.data_xufei.pay_way= $('input[name="xufei[pay_way]"]').val();
					        	if( $scope.customerInfo )
					        		$scope.data_xufei.contract_id = $scope.customerInfo.contract_id;
					        	xufei( $scope.data_xufei );
					        	return true;		        				
		        			}else{
				        		$('#xufei_type_add').addClass('error');
					  			var div=document.createElement("div");
			   					var diva = document.getElementById('xufei_add_sty');
			   					diva.parentNode.insertBefore(div, diva);			    
				        		$('#xufei_add_sty').text('续费类型只能为前续');
				        		$('#xufei_add_sty').addClass('ui basic red pointing prompt label transition visible');
					        	return false;	
		        			}		        			
		        		}else{
			        		$('#test_No').addClass('error');
			        		$('#add_sty').addClass('ui basic red pointing prompt label transition visible');
			        		document.getElementById("add_sty").innerHTML ='您的合同已经超过三个月,只能升级';
				        	return false;		        			
		        		}
		        	}else{
	        			if ($('input[name="xufei[xufei_type]"]').val() == 1) {
				        	$scope.data_xufei.product = $('input[name="xufei[product]"]').val();
				        	$scope.data_xufei.price = $('input[name="xufei[price]"]').val();
				        	$scope.data_xufei.xufei_type = $('input[name="xufei[xufei_type]"]').val();
				        	$scope.data_xufei.pay_way= $('input[name="xufei[pay_way]"]').val();
				        	if( $scope.customerInfo )
				        		$scope.data_xufei.contract_id = $scope.customerInfo.contract_id;
				        	xufei( $scope.data_xufei );
				        	return true;		        				
	        			}else{
			        		$('#xufei_type_add').addClass('error');
				  			var div=document.createElement("div");
		   					var diva = document.getElementById('xufei_add_sty');
		   					diva.parentNode.insertBefore(div, diva);			    
			        		$('#xufei_add_sty').text('续费类型只能为前续');
			        		$('#xufei_add_sty').addClass('ui basic red pointing prompt label transition visible');
				        	return false;	
	        			}		        		
		        	}
		        }
		        else{
		            return false;
		        }
		      }
		    }).modal('show');
		}
	

		//编辑新单查账 
		$scope.editOrder = function(){
			function edit( data ){
				$scope.busy = dataFactoryCRM.postDataByAPI('pays/editnew', data )
				      .then(function(d) {
				      	$scope.paysrcode = d.rcode;
				        if ( d && d.rcode) {
				          $scope.$emit("NOTBUSY");
				          $scope.getPaysList();
				          return true;
				        }
				        else{
                            $scope.$emit("NOTBUSY");
                            $scope.paysMsg = d.msg;
                            $scope.msgAttention();
				          	return false;
				        }
				      });
				      // $scope.getPaysList();
			}
			// 
			var _vpay = this.vpay;
			// $('.ui.editnew').modal("refresh");
			$scope.busy = dataFactoryCRM.getDataByAPI('pays/getserver?customer_sales='+ _vpay.customer_sales )
			      .then(function(d) {
			      	$('#customer_buycount1_value').val(d.nickname);
			});	
	 
			$('#customer_name_edit').val(_vpay.customer_name);
			$scope.customer_name_cat = _vpay.payrecord_id;
			$('#customer_phone_edit').val(_vpay.customer_phone);
			$('#customer_location_edit').val(_vpay.customer_location);
			$('#customer_sex_edit').val(_vpay.customer_sex);
			if (_vpay.customer_sex == 0) $('#customer_sex_edit1').text('未知');
			if (_vpay.customer_sex == 1) $('#customer_sex_edit1').text('男');
			if (_vpay.customer_sex == 2) $('#customer_sex_edit1').text('女');
			$('#customer_mount_edit').val(_vpay.customer_mount);
			$('#customer_qq_edit').val(_vpay.customer_qq);
			$('#customer_weixin_id_edit').val(_vpay.customer_weixin_id);
			$('#customer_weixin_nickname_edit').val(_vpay.customer_weixin_nickname);
			$('#customer_career_edit').val(_vpay.customer_career);
			for (var i = 0; i < $scope.careerTypes.length; i++) {
				if (_vpay.customer_career == $scope.careerTypes[i]['k']) $('#customer_career_edit1').text($scope.careerTypes[i]['v']);
			}

			$('#customer_education_edit').val(_vpay.customer_education);
			for (var i = 0; i < $scope.educationTypes.length; i++) {
				if (_vpay.customer_education == $scope.educationTypes[i]['k'] ) $('#customer_education_edit1').text($scope.educationTypes[i]['v']);
			}
			$('#customer_birth_edit').val(_vpay.customer_birth);
			$('#customer_source_edit').val(_vpay.customer_source);
			for (var i = 0; i < $scope.pageParam.sources.length; i++) {
				if (_vpay.customer_source == $scope.pageParam.sources[i]['source_id']) $('#customer_source_edit1').text($scope.pageParam.sources[i]['name']);				
			}
			$('#customer_address_edit').val(_vpay.customer_address);
			$('#customer_note_edit').val(_vpay.customer_note);
			$('#customer_product_edit').val(_vpay.customer_product);
			for (var i = 0; i < $scope.pageParam.products.length; i++) {
				if (_vpay.customer_product == $scope.pageParam.products[i]['product_id']) $('#customer_product_edit1').text($scope.pageParam.products[i]['name']);
			}
			$('#customer_price_edit').val(_vpay.customer_price);
			for (var i = 0; i < $scope.pageParam.prices.length; i++) {
				if (_vpay.customer_price == $scope.pageParam.prices[i]['price_id']) $('#customer_price_edit1').text($scope.pageParam.prices[i]['name']);
			}
			$('#contract_start_time_edit').val(_vpay.contract_start_time);
			$('#customer_buycount_edit').val(_vpay.customer_buycount);
			$('#customer_sales_edit').val(_vpay.customer_sales);//获取服务人员姓名
			$('#customer_sector_edit').val(_vpay.customer_sector);
			for (var i = 0; i < $scope.pageParam.sectors.length; i++) {
				if (_vpay.customer_sectors == $scope.pageParam.sectors[i]['sector_id'] ) $('#customer_sector_edit1').text($scope.pageParam.sectors[i]['sector_name']);
			}
			$('#pay_name_edit').val(_vpay.pay_name);
			$('#pay_mount_edit').val(_vpay.pay_mount);
			$('#pay_way_edit').val(_vpay.pay_way);
			for (var i = 0; i < $scope.pageParam.payways.length; i++) {
				if (_vpay.pay_way == $scope.pageParam.payways[i]['k']) $('#pay_way_edit1').text($scope.pageParam.payways[i]['v']);
			}
			$('#pay_time_edit').val(_vpay.pay_time);
			$('#note_edit').val(_vpay.note);

			$('.ui.edit').modal({
		      onApprove: function() {
		      	$scope.$emit("BUSY");
		        if( !$('#frm-editnew').form('is valid') ){
		            return false;
		        }
		        else{
		        	$scope.data_xindan.customer_name = $('input[name="customer_name_edit"]').val();
		        	$scope.data_xindan.customer_phone = $('input[name="customer_phone_edit"]').val();
		        	$scope.data_xindan.customer_location = $('input[name="customer_location_edit"]').val();
		        	$scope.data_xindan.customer_sex = $('input[name="customer_sex_edit"]').val();
		        	$scope.data_xindan.customer_mount = $('input[name="customer_mount_edit"]').val();
		        	$scope.data_xindan.customer_qq = $('input[name="customer_qq_edit"]').val();
		        	$scope.data_xindan.customer_weixin_id = $('input[name="customer_weixin_id_edit"]').val();
		        	$scope.data_xindan.customer_weixin_nickname = $('input[name="customer_weixin_nickname_edit"]').val();
		        	$scope.data_xindan.customer_career = $('input[name="customer_career_edit"]').val();
		        	$scope.data_xindan.customer_education = $('input[name="customer_education_edit"]').val();
		        	$scope.data_xindan.customer_birth = $('input[name="customer_birth_edit"]').val();
		        	$scope.data_xindan.customer_source = $('input[name="customer_source_edit"]').val();
		        	$scope.data_xindan.customer_address = $('input[name="customer_address_edit"]').val();
		        	$scope.data_xindan.customer_note = $('input[name="customer_note_edit"]').val();
		        	$scope.data_xindan.customer_product = $('input[name="customer_product_edit"]').val();
		        	$scope.data_xindan.customer_price = $('input[name="customer_price_edit"]').val();
		        	$scope.data_xindan.contract_start_time = $('input[name="contract_start_time_edit"]').val();
		        	$scope.data_xindan.customer_buycount = $('input[name="customer_buycount_edit"]').val();
		        	$scope.data_xindan.customer_sales = $('input[name="customer_sales_edit"]').val();
		        	// console.log($scope.data_xindan.customer_sales);
		        	$scope.data_xindan.customer_sector = $('input[name="customer_sector_edit"]').val();
		        	$scope.data_xindan.pay_name = $('input[name="pay_name_edit"]').val();
		        	$scope.data_xindan.pay_mount = $('input[name="pay_mount_edit"]').val();
		        	$scope.data_xindan.pay_way = $('input[name="pay_way_edit"]').val();
		        	$scope.data_xindan.pay_time = $('input[name="pay_time_edit"]').val();
		        	$scope.data_xindan.note = $('input[name="note_edit"]').val();
		        	$scope.data_xindan.payrecord_id = _vpay.payrecord_id;	
		        	edit( $scope.data_xindan );				        	
		        }
		        return true;
		      }
		    }).modal('show');			

		}

		//编辑续费查账
		$scope.editxufeiOrder = function(){
			var _vpay = this.vpay;
			$('#xufei_customer_edit').val(_vpay.customer_id);
			$('#xufei_price_edit').val(_vpay.customer_sales);
			$('#xufei_type_edit').val(_vpay.xufei_type);
			$('#xufei_contract_start_time_edit').val(_vpay.contract_start_time);
			$('#xufei_customer_buycount_edit').val(_vpay.customer_buycount);
			$('#xufei_xufei_type_edit').val(_vpay.xufei_type);
			if (_vpay.xufei_type == 0) $('#xufei_xufei_type_edit1').text('补款');
			if (_vpay.xufei_type == 1) $('#xufei_xufei_type_edit1').text('前续');
			if (_vpay.xufei_type == 2) $('#xufei_xufei_type_edit1').text('后续');		
			$('#xufei_pay_name_edit').val(_vpay.pay_name);
			$('#xufei_pay_mount_edit').val(_vpay.pay_mount);
			$('#xufei_pay_way_edit').val(_vpay.pay_way);
			$('#xufei_pay_way_edit1').text(_vpay.pay_way);
			$('#xufei_product_edit').val(_vpay.customer_product);
			for (var i = 0; i < $scope.pageParam.products.length; i++) {
				if (_vpay.customer_product == $scope.pageParam.products[i]['product_id'] ) $('#xufei_product_edit1').text($scope.pageParam.products[i]['name']);
			}
			$('#xufei_price_edit').val(_vpay.customer_price);
			for (var i = 0; i < $scope.pageParam.prices.length; i++) {
				if (_vpay.customer_price == $scope.pageParam.prices[i]['price_id'] ) $('#xufei_price_edit1').text($scope.pageParam.prices[i]['name']);
			}
			$('#xufei_pay_time_edit').val(_vpay.pay_time);
			$('#xufei_note_edit').val(_vpay.note);
			// 显示合同信息
			var c_id = _vpay.customer_id;
			dataFactoryCRM.postDataByAPI('pays/products','customer='+c_id)
	            .then(function(d) {
	              if (d.rcode) {
		              	$scope.customerInfo1 = d.data;
		              	$scope.customerVerify1 = 1;
		                $scope.d_times_xufei = d.data.d_time;
		                $scope.Pro_id_xufei = d.data.product_id;

	              } else {
	                $scope.customerInfo1 = {};
	                $scope.customerVerify1 = 0;
	                // $scope.data_xufei.customer = -1;
	              }
            });

			dataFactoryCRM.postDataByAPI('pays/getlogin_name','customer='+c_id)
	            .then(function(d) {
				$('#xufei_customer_edit1_value').val(d.login_name);
            });				
			function xufei_edit( data ){
				$scope.busy = dataFactoryCRM.postDataByAPI('pays/editxufei', data )
				      .then(function(d) {
				      	$scope.paysrcode = d.rcode;
				        if ( d && d.rcode) {
				        	 $scope.$emit("NOTBUSY");
				          	 $scope.getPaysList();
				          	return true;
				        }
				        else
                            $scope.$emit("NOTBUSY");
                            $scope.paysMsg = d.msg;
                            $scope.msgAttention();
                            return false;
				      });
				      // $scope.getPaysList();
			}
			$('.ui.xufei_edit').modal({
		      onApprove: function() {
		      	$scope.$emit("BUSY");
		        if( $('#frm-editxufei').form('is valid') ){
		        	if ($scope.d_times_xufei == 1) {
		        		if ($scope.ConState_xufei == -1 || $scope.ConState_xufei == 2 || $scope.ConState_xufei == 3) {
		        			if ($('#xufei_type_edit').val() == 1) {
					        	$scope.data_xufei1.customer_buycount = $('input[name="xufei[customer_buycount]_edit"]').val();
					        	$scope.data_xufei1.contract_start_time = $('input[name="xufei[contract_start_time]_edit"]').val();
					        	$scope.data_xufei1.pay_mount = $('input[name="xufei[pay_mount]_edit"]').val();
					        	$scope.data_xufei1.pay_name = $('input[name="xufei[pay_name]_edit"]').val();
					        	$scope.data_xufei1.customer_product = $('input[name="xufei[product]_edit"]').val();
					        	$scope.data_xufei1.customer_price = $('input[name="xufei[price]_edit"]').val();
					        	$scope.data_xufei1.xufei_type = $('input[name="xufei[xufei_type]_edit"]').val();
					        	$scope.data_xufei1.pay_way= $('input[name="xufei[pay_way]_edit"]').val();
					        	$scope.data_xufei1.note = $('input[name="xufei[note]_edit"]').val();
					        	$scope.data_xufei1.pay_time = $('input[name="xufei[pay_time]_edit"]').val();
					        	$scope.data_xufei1.customer_id = $('input[name="xufei[customer_id]_edit"]').val();
					        	$scope.data_xufei1.payrecord_id = _vpay.payrecord_id;
					        	xufei_edit( $scope.data_xufei1 );
					        	return true;			        				
		        			}else{
			        			$scope.$emit("NOTBUSY");
				        		$('#xufei_type_add_xufei').addClass('error');
					  			var div_xufei=document.createElement("div");
			   					var diva_xufei = document.getElementById('xufei_type_add_xufei');
			   					diva_xufei.parentNode.insertBefore(div_xufei, diva_xufei);			    
				        		$('#xufei_type_add_xufei').text('续费类型只能为前续');
				        		$('#xufei_type_add_xufei').addClass('ui basic red pointing prompt label transition visible');
					        	return false;			        				
		        			}
		        			
		        		}else{
			        		if ($('#xufei_product_edit').val() == $scope.Pro_id_xufei && $('#xufei_type_edit').val() == 0) {
					        	$scope.data_xufei1.customer_buycount = $('input[name="xufei[customer_buycount]_edit"]').val();
					        	$scope.data_xufei1.contract_start_time = $('input[name="xufei[contract_start_time]_edit"]').val();
					        	$scope.data_xufei1.pay_mount = $('input[name="xufei[pay_mount]_edit"]').val();
					        	$scope.data_xufei1.pay_name = $('input[name="xufei[pay_name]_edit"]').val();
					        	$scope.data_xufei1.customer_product = $('input[name="xufei[product]_edit"]').val();
					        	$scope.data_xufei1.customer_price = $('input[name="xufei[price]_edit"]').val();
					        	$scope.data_xufei1.xufei_type = $('input[name="xufei[xufei_type]_edit"]').val();
					        	$scope.data_xufei1.pay_way= $('input[name="xufei[pay_way]_edit"]').val();
					        	$scope.data_xufei1.note = $('input[name="xufei[note]_edit"]').val();
					        	$scope.data_xufei1.pay_time = $('input[name="xufei[pay_time]_edit"]').val();
					        	$scope.data_xufei1.customer_id = $('input[name="xufei[customer_id]_edit"]').val();
					        	$scope.data_xufei1.payrecord_id = _vpay.payrecord_id;
					        	xufei_edit( $scope.data_xufei1 );
					        	return true;		        			
			        		}else if($('#xufei_product_edit').val() != $scope.Pro_id_xufei){
					        	$scope.data_xufei1.customer_buycount = $('input[name="xufei[customer_buycount]_edit"]').val();
					        	$scope.data_xufei1.contract_start_time = $('input[name="xufei[contract_start_time]_edit"]').val();
					        	$scope.data_xufei1.pay_mount = $('input[name="xufei[pay_mount]_edit"]').val();
					        	$scope.data_xufei1.pay_name = $('input[name="xufei[pay_name]_edit"]').val();
					        	$scope.data_xufei1.customer_product = $('input[name="xufei[product]_edit"]').val();
					        	$scope.data_xufei1.customer_price = $('input[name="xufei[price]_edit"]').val();
					        	$scope.data_xufei1.xufei_type = $('input[name="xufei[xufei_type]_edit"]').val();
					        	$scope.data_xufei1.pay_way= $('input[name="xufei[pay_way]_edit"]').val();
					        	$scope.data_xufei1.note = $('input[name="xufei[note]_edit"]').val();
					        	$scope.data_xufei1.pay_time = $('input[name="xufei[pay_time]_edit"]').val();
					        	$scope.data_xufei1.customer_id = $('input[name="xufei[customer_id]_edit"]').val();
					        	$scope.data_xufei1.payrecord_id = _vpay.payrecord_id;
					        	xufei_edit( $scope.data_xufei1 );
					        	return true;
			        		}else{
			        			$scope.$emit("NOTBUSY");
				        		$('#xufei_type_add_xufei').addClass('error');
					  			var div_xufei=document.createElement("div");
			   					var diva_xufei = document.getElementById('xufei_type_add_xufei');
			   					diva_xufei.parentNode.insertBefore(div_xufei, diva_xufei);			    
				        		$('#xufei_type_add_xufei').text('续费类型只能为补款');
				        		$('#xufei_type_add_xufei').addClass('ui basic red pointing prompt label transition visible');
					        	return false;			        			
			        		}		        			
		        		}
		        	}else if($scope.d_times_xufei == 2){
		        		if ($scope.ConState_xufei == -1 || $scope.ConState_xufei == 2 || $scope.ConState_xufei == 3) {
				        	$scope.data_xufei1.customer_buycount = $('input[name="xufei[customer_buycount]_edit"]').val();
				        	$scope.data_xufei1.contract_start_time = $('input[name="xufei[contract_start_time]_edit"]').val();
				        	$scope.data_xufei1.pay_mount = $('input[name="xufei[pay_mount]_edit"]').val();
				        	$scope.data_xufei1.pay_name = $('input[name="xufei[pay_name]_edit"]').val();
				        	$scope.data_xufei1.customer_product = $('input[name="xufei[product]_edit"]').val();
				        	$scope.data_xufei1.customer_price = $('input[name="xufei[price]_edit"]').val();
				        	$scope.data_xufei1.xufei_type = $('input[name="xufei[xufei_type]_edit"]').val();
				        	$scope.data_xufei1.pay_way= $('input[name="xufei[pay_way]_edit"]').val();
				        	$scope.data_xufei1.note = $('input[name="xufei[note]_edit"]').val();
				        	$scope.data_xufei1.pay_time = $('input[name="xufei[pay_time]_edit"]').val();
				        	$scope.data_xufei1.customer_id = $('input[name="xufei[customer_id]_edit"]').val();
				        	$scope.data_xufei1.payrecord_id = _vpay.payrecord_id;
				        	xufei_edit( $scope.data_xufei1 );
				        	return true;		        			
		        		}else{
			        		$scope.$emit("NOTBUSY");
			        		$('#test_No_xufei').addClass('error');
			        		$('#add_sty_xufei').addClass('ui basic red pointing prompt label transition visible');
			        		document.getElementById("add_sty_xufei").innerHTML ='您的合同已经超过三个月,只能升级';
				        	return false;		        			
		        		}
		        	}else{
			        	$scope.data_xufei1.customer_buycount = $('input[name="xufei[customer_buycount]_edit"]').val();
			        	$scope.data_xufei1.contract_start_time = $('input[name="xufei[contract_start_time]_edit"]').val();
			        	$scope.data_xufei1.pay_mount = $('input[name="xufei[pay_mount]_edit"]').val();
			        	$scope.data_xufei1.pay_name = $('input[name="xufei[pay_name]_edit"]').val();
			        	$scope.data_xufei1.customer_product = $('input[name="xufei[product]_edit"]').val();
			        	$scope.data_xufei1.customer_price = $('input[name="xufei[price]_edit"]').val();
			        	$scope.data_xufei1.xufei_type = $('input[name="xufei[xufei_type]_edit"]').val();
			        	$scope.data_xufei1.pay_way= $('input[name="xufei[pay_way]_edit"]').val();
			        	$scope.data_xufei1.note = $('input[name="xufei[note]_edit"]').val();
			        	$scope.data_xufei1.pay_time = $('input[name="xufei[pay_time]_edit"]').val();
			        	$scope.data_xufei1.customer_id = $('input[name="xufei[customer_id]_edit"]').val();
			        	$scope.data_xufei1.payrecord_id = _vpay.payrecord_id;
			        	xufei_edit( $scope.data_xufei1 );
			        	return true;	        		
		        	}

		        }
		        else{
		            return false;
		        }
		      }
		    }).modal('show');
		}
		$scope.getOptions();
		$scope.getPaysList();

		// 添加退款
		$scope.feedback = function(){
			function add( data ){
				$scope.busy = dataFactoryCRM.postDataByAPI('pays/addfeedback', data )
				      .then(function(d) {
				      	// console.log(d);
				        if ( d && d.d) {
				          // $('#frm-feedback input').val('');
				          $scope.getPaysList();
				          return true;
				        }
				        else{
				        	alert(d.i);
				          	return false;
				        }
				         $scope.getPaysList();
				      });
			}
			// $('.ui.feedback').modal("refresh");
			$('.ui.feedback').modal({
		      onApprove: function() {
		        if( !$('#frm-feedback').form('is valid') ){
		            return false;
		        }
		        else{
		        	add( $('#frm-feedback').serialize() );
		        }
		        return true;
		      }
		    }).modal('show');			
		}

		// 编辑退款查账
		$scope.returnOrder = function(){
			var _vp = this.vpay;
			// console.log(_vp);
			$('#customer_fedbak_alt1_value').val(_vp.customer_name);
			$('#customer_fedbak_edit').val(_vp.customer_id);
			$('#feedbackmount_edit').val(_vp.pay_mount);
			$('#feedbacknote_eidt').val(_vp.note);
			$('#payrecord_fed_id').val(_vp.payrecord_id);
			var c_id_edit = _vp.customer_id;
			dataFactoryCRM.postDataByAPI('pays/products','customer='+c_id_edit)
	            .then(function(d) {
	              if (d.rcode) {
		              	$scope.customerInfo = d.data;
		                $scope.d_times_back = d.data.d_time;
		                $scope.Pro_id_back = d.data.product_id;

	              } else {
	                $scope.customerInfo = {};
	                // $scope.data_xufei.customer = -1;
	              }
            });	
			function add( data ){
				$scope.busy = dataFactoryCRM.postDataByAPI('pays/RetMoney', data )
				      .then(function(d) {
				      	$scope.paysrcode = d.rcode;
				        if ( d && d.rcode) {
				          $('#frm-editback input').val('');
				          $scope.getPaysList();
				          return true;
				        }
				        else{
                            $scope.$emit("NOTBUSY");
                            $scope.productMsg = d.msg;
                            $scope.msgAttention();				   
				          	return false;
				        }
				      });
				      // $scope.getPaysList();
			}
			// $('.ui.editback').modal("refresh");
			$('.ui.editback').modal({
		      onApprove: function() {
		      	$scope.$emit("BUSY");
		        if( $('#frm-editback').form('is valid') ){
		        	add( $('#frm-editback').serialize() );
		        }
		        else{
		            return false;
		        }
		        return true;
		      }
		    }).modal('show');				
		}

		//删除订单
		 $scope.deleteOrder = function(){
		 	var _del = this.vpay;
		 	// console.log(_del);
            $('#del_feedback .content p').html("确认删除" + _del.pay_name + _del.pay_time );
            $('.ui.small.del').modal({
            	onApprove:function(){
            		$scope.$emit("BUSY");
	                dataFactoryCRM.getDataByAPI('Pays/del_feedback?payrecord_id='+ _del.payrecord_id )
	                  .then(function(d){
	                  	$scope.paysrcode = d.rcode;
	                  if( d.rcode ) {
	                    if( $scope.payslist ){
	                      var i = 0;
	                      for(;i<$scope.payslist.length; i++){
	                        if( $scope.payslist[i].payrecord_id == _del.payrecord_id){
	                          $scope.payslist.splice(i,1);
	                          break;
	                        }
	                      }
	                    }
	                    // return true;
	                  }else{
                        $scope.$emit("NOTBUSY");
                        $scope.productMsg = d.msg;
                        $scope.msgAttention();
	                  }
	                  $scope.$emit("NOTBUSY");
	                 });
	                      return true;
            	},
                  onDeny: function() {
                  	return true;
            }}).modal('show'); 
		 }

	// 新单查看审查
		$scope.checknew = function(){
			var _va  = this.vpay;
			$scope.busy = dataFactoryCRM.postDataByAPI('pays/getCustomerMsg','payrecord_id=' + _va.payrecord_id )
			      .then(function(d) {
			      	$('#customer_name_newcheck').val(d.customer_name);
			      	$('#customer_phone_newcheck').val(d.customer_phone);
			      	$('#customer_location_newcheck').val(d.customer_location);
			      	if (d.customer_sex == 1) $('#customer_sex_newcheck').text('女性');
			      	if (d.customer_sex == 2) $('#customer_sex_newcheck').text('男性');
			      	if (d.customer_sex == 0) $('#customer_sex_newcheck').text('未知');
			      	$('#customer_mount_newcheck').val(d.customer_mount);
			      	$('#customer_qq_newcheck').val(d.customer_qq);
			      	$('#customer_weixin_id_newcheck').val(d.customer_weixin_id);
			      	$('#customer_weixin_nickname_newcheck').val(d.customer_weixin_nickname);
					for (var i = 0; i < $scope.careerTypes.length; i++) {
						if (d.customer_career == $scope.careerTypes[i]['k']) $('#customer_career_check').text($scope.careerTypes[i]['v']);
					}
					for (var i = 0; i < $scope.educationTypes.length; i++) {
						if (d.customer_career == $scope.educationTypes[i]['k']) $('#customer_education_newcheck').text($scope.educationTypes[i]['v']);
					}	
					$('#customer_birth_newcheck').val(d.customer_birth);
					for (var i = 0; i < $scope.pageParam.sources.length; i++) {
						if (d.customer_source == $scope.pageParam.sources[i]['source_id']) $('#customer_source_newcheck').text($scope.pageParam.sources[i]['name']);
					}
					$('#customer_address_newcheck').val(d.customer_address);
					$('#customer_note_newcheck').val(d.customer_note);
					for (var i = 0; i < $scope.pageParam.products.length; i++) {
						if (d.customer_product == $scope.pageParam.products[i]['product_id']) $('#customer_product_newcheck').text($scope.pageParam.products[i]['name']);
					}
					for (var i = 0; i < $scope.pageParam.prices.length; i++) {
						if (d.customer_price == $scope.pageParam.prices[i]['price_id']) $('#customer_price_newcheck').text($scope.pageParam.prices[i]['name']);
					}
					$('#contract_start_time_newcheck').val(d.contract_start_time);
					$('#customer_buycount_newcheck').val(d.customer_buycount);
					$('#customer_user_newcheck_value').val(d.unickname);
					$('#customer_sector_newcheck').text(d.sector_name);
					$('#pay_name_newcheck').val(d.pay_name);
					$('#pay_mount_newcheck').val(d.pay_mount);
					for (var i = 0; i < $scope.pageParam.payways.length; i++) {
						if (d.pay_way == $scope.pageParam.payways[i]['k']) $('#pay_way_newcheck').text($scope.pageParam.payways[i]['v']);
					}					
					$('#pay_time_newcheck').val(d.pay_time);
					$('#note_newcheck').val(d.customer_note);						
			      });
			$('.ui.newcheck').modal({
		      onApprove: function() {
		        return true;
		      }
		    }).modal('show');			
		}

	// 续费查看审查
		$scope.xufei_check = function(){
			var _vpa = this.vpay;
			// console.log(_vpa);
			$scope.busy = dataFactoryCRM.postDataByAPI('pays/getCustomerMsg_xufei','payrecord_id=' + _vpa.payrecord_id )
			      .then(function(data) {
					dataFactoryCRM.postDataByAPI('pays/products','customer='+ data.customer_id)
			            .then(function(ds) {
			              if (ds.rcode) {
				              	$scope.customerInfo_check = ds.data;
			              } else {
			                $scope.customerInfo_check = {};
			              }
		            });	
			      	$('#xufei_note_check').val(data.note);
			      	$('#xufei_customer_name_check_value').val(data.login_name);
			      	$('#xufei_productcheck_text').text(data.pro_name);
			      	$('#xufei_price_text').text(data.name);
			      	$('#xufei_contractstart_time_check').val(data.contract_start_time);
			      	$('#xufei_customer_buycount_check').val(data.customer_buycount);
					if (data.xufei_type == 0) $('#xufei_xufeitype_text').text('补款');
					if (data.xufei_type == 1) $('#xufei_xufeitype_text').text('前续');
					if (data.xufei_type == 2) $('#xufei_xufeitype_text').text('后续');	
					$('#xufei_payname_check').val(data.pay_name);
					$('#xufei_paymount_check').val(data.pay_mount);		      	
					$('#xufei_paystyle_check').text(data.pay_way);
					$('#xufei_pay_time_check').val(data.pay_time);

			      });
			$('.ui.xufeicheck').modal("refresh");
			$('.ui.xufeicheck').modal({
		      onApprove: function() {
		        return true;
		      }
		    }).modal('show');			
		}

	//查看退款详情
		$scope.reback_check = function(){
				var _vm = this.vpay;		
				$scope.busy = dataFactoryCRM.postDataByAPI('pays/feccheck', 'payrecord_id=' + _vm.payrecord_id )
				      .then(function(da) {
				      	// console.log(da);
						dataFactoryCRM.postDataByAPI('pays/products','customer='+ da.customer_id)
				            .then(function(dm) {
				              if (dm.rcode) {
					              	$scope.customerInfo_check = dm.data;
				              } else {
				                $scope.customerInfo_check = {};
				              }
			            });	

				        $('#customer_fedbak_check_value').val(da.nickname);
				        $('#feedbackmount_check').val(da.pay_mount);
				        $('#feedbacknote_check').val(da.note); 
				      });
				$('.ui.fedcheck').modal("refresh");
				$('.ui.fedcheck').modal({
			      onApprove: function() {
			        return true;
			      }
			    }).modal('show');			
		} 
	 });
});